gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\learning\minimax\mmdemo.m
function []=mmdemo(action,hfigure,varargin) % MMDEMO demo on the Minimax learning algorithm. % % MMDEMO demonstrates the Minimax learning algorithm on % a simple examples in 2-dimensional feature space. % % Minimax learning algorithm for Gaussian model estimates % a covarinace matrix and a mean value of the Gaussian from % given traning set. The training set is supposed to contain % patterns well describing the problem (having high value % of p.d.f). The patterns need not to be randomly and % independently selected. % % Informaly speeking, this algorihm search for an ellipsoid % containing the point set and having minimal diameter % (see help mmln for more details). % % The found model is described by an ellipsoid (shape of % covariance) and a crose (mean value vector). The upper (blue) % and lower (red) bound of the optimal solution is displayed % as well. % % Controling: % Epsilon - stop condition of the algorithm - a difference between % upper and lower bound of the optimal solution. % Iterations - number of iterations in one step. % % FIG2EPS - export screen to the PostScript file. % Load data - load input point sets from file. % Create data - invoke program for creating point sets. % Reset - set the tested algorithm to the initial state. % Play - run the tested algorithm. % Stop - stop the running algorithm. % Step - perform only one step. % Info - invoke the info box. % Close - close the program. % % See also MMLN. % % Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac % (c) Czech Technical University Prague, http://cmp.felk.cvut.cz % Written Vojtech Franc (diploma thesis) 10.12.1999, 27.02.2000, 5.4.2000 % Modifications % 11-june-2001, V.Franc, comments added. % 24. 6.00 V. Hlavac, comments polished. % Used functions: PPOINTS, ELLIPS BORDER=0.25; % space between window limits and the points CENTERSIZE=10; % size of center point LINE_WIDTH=1; AXIST_ADD=10; DATA_IDENT='Finite sets, Enumeration'; % file identifier if nargin < 1, action = 'initialize'; end % what action is required ? switch lower(action) case 'initialize' % == Initialize user interface control and figure window ================ % == Figure ============================================================= left=0.2; width=0.6; bottom=0.1; height=0.8; hfigure=figure('Name','Minimax learning', ... 'Visible','off',... 'NumberTitle','off', ... 'Units','normalized', ... 'Position',[left bottom width height],... 'tag','Mmdemo',... 'doublebuffer','on',... 'backingstore','off'); % == Axes =============================================================== % axes with prob. left=0.1; width=0.65; bottom=0.1; height=0.28; haprob=axes(... 'Units','normalized', ... 'NextPlot','add',... 'Position',[left bottom width height]); title('blue - log p(x), red - \Sigma \alpha(x) log p(x)',... 'Parent',haprob,... 'VerticalAlignment','bottom',... 'Units','normalized',... 'HorizontalAlignment','left',... 'Position',[0 1 0]); htxsteps=xlabel('step number'); % points height=0.45; bottom=0.5; % 'XTick',[],'YTick',[], ... haset=axes(... 'Units','normalized', ... 'NextPlot','add',... 'Position',[left bottom width height]); ylabel('feature y'); xlabel('feature x'); % == Buttons =========================================================== % -- Export to EPS --------- width=0.1; left=0.75-width; bottom=0.95; height=0.04; hbtclose = uicontrol(... 'Units','Normalized', ... 'Callback','fig2eps(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','FIG2EPS'); %---------------------------------- % Close button left=0.8; bottom=0.05; height=0.05; width=0.15; hbtclose = uicontrol(... 'Units','Normalized', ... 'Callback','close(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Close'); % Info button: call stanard info box bottom=bottom+1.5*height; hbtinfo = uicontrol(... 'Units','Normalized', ... 'Callback','mmdemo(''info'',gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Info'); % Step button: perform one adaptation step bottom=bottom+1.5*height; hbtstep = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Step', ... 'Interruptible','off',... 'Callback','mmdemo(''step'',gcf)'); % Stop button: stop process of adaptation bottom=bottom+height; hbtstop = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Stop', ... 'Callback','set(gcbo,''UserData'',1)',... 'Enable','off'); % Play button: start up adaptation bottom=bottom+height; hbtplay = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Play', ... 'Callback','mmdemo(''play'',gcf)'); % Reset button: set up t = 0 bottom=bottom+height; hbtreset = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Reset', ... 'Callback','mmdemo(''reset'',gcf)'); % Create data bottom=bottom+1.5*height; hbtcreat = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Create data', ... 'Callback','mmdemo(''creatdata'',gcf)'); % Load data bottom=bottom+1*height; hbtload = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Load data', ... 'Callback','mmdemo(''getfile'',gcf)'); % == Edit line ========================================================== % epsilon bottom=0.95-height; htxeps=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'String','epsilon'); bottom=bottom-height; hedeps = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'String','0.1'); % Iterations bottom=bottom-1.5*height; htxiter=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'String','Iterations'); bottom=bottom-height; hediter = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'String','1'); % == Texts =========================================================== htitle1=title('No data loaded',... 'Parent',haset,... 'VerticalAlignment','bottom',... 'HorizontalAlignment','left',... 'Units','normalized',... 'Position',[0 1 0]); %===================================================================== % Store handlers handlers=struct(... 'ellipse',struct('handler',-1,'center',-1,'mi',[],'sigma',[],'t',0,'N',[]),... 'plot1',struct('handler',-1,'topps',[],'axist',0,'time',[]),... 'plot2',struct('handler',-1,'minps',[]),... 'title1',htitle1,... 'btstep',hbtstep,... 'btstop',hbtstop,... 'btclose',hbtclose,... 'btplay',hbtplay,... 'btreset',hbtreset,... 'btinfo',hbtinfo,... 'btload',hbtload,... 'btcreat',hbtcreat,... 'txsteps',htxsteps,... 'txeps',htxeps,... 'txiter',htxiter,... 'aset',haset,... 'aprob',haprob,... 'editer',hediter,... 'edeps',hedeps); set(hfigure,'UserData',handlers) % Reset mmdemo('reset',hfigure); % Put figure on desktop set(hfigure,'Visible','on'); drawnow; case 'play' % == One step learning ============================================== h=get(hfigure,'UserData'); % get data set sets=get(h.aset,'UserData'); % are data sets loaded ? if isempty(sets)==1, return; end % disable button set([h.editer,h.edeps,h.btstep,h.btclose,h.btplay,... h.btreset,h.btinfo,h.btload,h.btcreat,h.txeps,h.txiter],... 'Enable','off'); % enable stop button set(h.btstop,'Enable','on'); % get # of iter and epsilon iter=str2num(get(h.editer,'String')); epsilon=str2num(get(h.edeps,'String')); % set stop button set(h.btstop,'UserData',0); % start point for plot if h.ellipse.t==0 & iter > 1, [mi,sigma,solution,minp,topp]=mmln(sets.X,epsilon,1,0); h.plot1.time=[1]; h.plot2.minps=[minp]; h.plot1.topps=[topp]; end % Play - adaptation process play=1; while play==1 & get(h.btstop,'UserData')==0, % perform one learning step [mi,sigma,solution,minp,topp,h.ellipse.N,h.ellipse.t]=... mmln(sets.X,epsilon,iter,h.ellipse.t,h.ellipse.N); text=sprintf('step number t=%d ',h.ellipse.t); if solution==1, text=strcat(text,',solution was found.'); play=0; else % add new value to plot h.plot1.time=[h.plot1.time,h.ellipse.t]; h.plot2.minps=[h.plot2.minps,minp]; h.plot1.topps=[h.plot1.topps,topp]; % is axis to be changed ? if h.ellipse.t > h.plot1.axist, h.plot1.axist=h.ellipse.t+iter*AXIST_ADD; set(h.aprob,'XLim',[1 h.plot1.axist]); end set(h.plot2.handler,'XData',h.plot1.time,'YData',h.plot2.minps,'Visible','on'); set(h.plot1.handler,'XData',h.plot1.time,'YData',h.plot1.topps,'Visible','on'); % erase old ellipse, compute and plot new one set(h.ellipse.center,'XData',mi(1),'YData',mi(2),'Visible','on'); r=sqrt(max(mahalan(sets.X,mi,sigma))); %%% [x,y]=ellipse(inv(sigma),50,r,mi); [x,y]=ellips(mi,sigma,r,40); set(h.ellipse.handler,'XData',x,'YData',y,'Visible','on'); end % comment set(h.txsteps,'String',text); % store data set(hfigure,'UserData',h); % flush it on desktop drawnow; end % disable button set([h.edeps,h.editer,h.btstep,h.btclose,h.btplay,... h.btreset,h.btinfo,h.btload,h.btcreat,h.txeps,h.txiter],... 'Enable','on'); % enable stop button set(h.btstop,'Enable','off'); case 'step' % == One step learning ============================================== h=get(hfigure,'UserData'); % get data set sets=get(h.aset,'UserData'); % are data sets loaded ? if isempty(sets)==1, return; end % get # of iter and epsilon iter=str2num(get(h.editer,'String')); epsilon=str2num(get(h.edeps,'String')); % start point for plot if h.ellipse.t==0 & iter > 1, [mi,sigma,solution,minp,topp]=mmln(sets.X,epsilon,1,0); h.plot1.time=[1]; h.plot2.minps=[minp]; h.plot1.topps=[topp]; end % perform one learning step [mi,sigma,solution,minp,topp,h.ellipse.N,h.ellipse.t]=... mmln(sets.X,epsilon,iter,h.ellipse.t,h.ellipse.N); text=sprintf('step number t=%d ',h.ellipse.t); if solution==1, text=strcat(text,',solution was found.'); else % add new value to plot h.plot1.time=[h.plot1.time,h.ellipse.t]; h.plot2.minps=[h.plot2.minps,minp]; h.plot1.topps=[h.plot1.topps,topp]; % is axis to be changed ? if h.ellipse.t > h.plot1.axist, h.plot1.axist=h.ellipse.t+iter*AXIST_ADD; set(h.aprob,'XLim',[1 h.plot1.axist]); end set(h.plot2.handler,'XData',h.plot1.time,'YData',h.plot2.minps,'Visible','on'); set(h.plot1.handler,'XData',h.plot1.time,'YData',h.plot1.topps,'Visible','on'); % erase old ellipse, compute and plot new one set(h.ellipse.center,'XData',mi(1),'YData',mi(2),'Visible','on'); r=sqrt(max(mahalan(sets.X,mi,sigma))); %%% [x,y]=ellipse(inv(sigma),50,r,mi); [x,y]=ellips(mi,sigma,r,40); set(h.ellipse.handler,'XData',x,'YData',y,'Visible','on'); end % comment set(h.txsteps,'String',text); % flush it on desktop drawnow; set(hfigure,'UserData',h); case 'redraw' % == Redraw contents of axes ==================================== h=get(hfigure,'UserData'); % uicontrol handlers % get sets with points sets=get(h.aset,'UserData'); if isempty(sets)==1, return; end % clears axes axes(h.aset); set(get(h.aset,'Children'),'EraseMode','normal'); %%% cla; clrchild(h.aset); h.ellipse.handler=plot([0],[0],... 'Parent',h.aset,'LineWidth',LINE_WIDTH,... 'EraseMode','xor','Color','k','Visible','off'); h.ellipse.center=line(0,0,'Marker','x',... 'EraseMode','xor','Color','k','MarkerSize',CENTERSIZE,'Visible','off'); win=cmpwin(min(sets.X'),max(sets.X'),BORDER,BORDER); %%% axis(win); setaxis(h.aset,win); axes(h.aset); % plot mixture %% pplot(sets.X,sets.I); ppoints(sets.X,sets.I); set(hfigure,'UserData',h); case 'getfile' % == Invoke standard open file dialog ==================================== % Opens file and checks if contains appropriate data, if yes than loads data. h=get(hfigure,'UserData'); % change path to directory %% wres=what('minimax'); %% cd(wres.path); [name,path]=uigetfile('*.mat','Open file'); if name~=0, file.pathname=strcat(path,name); file.path=path; file.name=name; if checkdat(file.pathname,DATA_IDENT,2,[0])==1, set(h.btload,'UserData',file); mmdemo('loadsets',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end end case 'loadsets' % == Load sets ================================================================== % Get file name from the pop up menu according to menu pointer. h=get(hfigure,'UserData'); % Get file name with sets file=get(h.btload,'UserData'); % Load sets sets=load(file.pathname); % store loaded sets set(h.aset,'UserData',sets); % call reset mmdemo('reset',hfigure); % call redraw mmdemo('redraw',hfigure); drawnow; case 'reset' % == Reset adaptation process, set up t=0 ================ h=get(hfigure,'UserData'); % get handlers % get file file=get(h.btload,'UserData'); % get data set sets=get(h.aset,'UserData'); % zeroes parameters of the separation line h.ellipse.mi=[]; h.ellipse.sigma=[]; h.ellipse.t=0; h.ellipse.N=[]; h.plot1.topps=[]; h.plot2.minps=[]; h.plot1.time=[]; h.plot1.axist=0; % comment text=sprintf('step number t=0 '); set(h.txsteps,'String',text); % hide ellipse if h.ellipse.handler==-1, h.ellipse.handler=plot([0],[0],... 'Parent',h.aset,'LineWidth',LINE_WIDTH,... 'EraseMode','xor','Color','k','Visible','off'); h.ellipse.center=line(0,0,'Marker','x',... 'EraseMode','xor','Color','k','MarkerSize',CENTERSIZE,'Visible','off'); else set(h.ellipse.handler,'Visible','off'); set(h.ellipse.center,'Visible','off'); end % if h.ellipse.handler~=-1 % clear axes prob. axes(h.aprob); cla; axis auto; h.plot1.handler=plot([0],[0],'b','Parent',h.aprob,... 'EraseMode','background','Visible','off'); h.plot2.handler=plot([0],[0],'r','Parent',h.aprob,... 'EraseMode','background','Visible','off'); % set up handlers and flush queue with graph. objects set(hfigure,'UserData',h); % creat comment axes(h.aset); if isempty(sets)==0, set(h.title1,'String',sprintf('File: %s, # of points K = %d',file.name,sum(sets.K))); else set(h.title1,'String','No data loaded'); pos=get(h.aset,'Position'); fsize=min(pos(3),pos(4))/8; axis([-1 1 -1 1]); builtin('text',0,0,'Press ''Load data'' button.',... 'HorizontalAlignment','center',... 'FontUnits','normalized',... 'Clipping','on',... 'FontSize',fsize); end drawnow; case 'creatdata' % == Invoke data set creator ============================================ creatset('finite',1,'mmdemo','created',hfigure); case 'created' % == Load new created data set =========================================== % get handler and make this figure active figure(hfigure); h=get(hfigure,'UserData'); % get file name path=varargin{1}; name=varargin{2}; pathname=strcat(path,name); if checkdat(pathname,DATA_IDENT,2,[0])==1, file.pathname=pathname; file.path=path; file.name=name; set(h.btload,'UserData',file); mmdemo('loadsets',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end case 'info' % == Call standard Matlab`s info box ========================================= helpwin(mfilename); end % switch